/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.deployment.manager;

import br.com.deployment.entidade.registroimovel.Averbacao;
import br.com.deployment.entidade.registroimovel.Imovel;
import br.com.deployment.entidade.registroimovel.Transferencia;
import br.com.deployment.entidade.acesso.Usuario;
import br.com.deployment.message.Msg;
import br.com.deployment.servico.AverbacaoServico;
import br.com.deployment.servico.ImovelServico;
import br.com.deployment.servico.TransferenciaServico;
import br.com.deployment.servico.UsuarioServico;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author elyjr
 */
@ManagedBean
@SessionScoped
public class ManagerAverbacao implements Serializable{

    @EJB
    AverbacaoServico averbacaoServico;
    
    @EJB
    TransferenciaServico transferenciaServico;
            
    @EJB
    ImovelServico imovelServico;
    
        
    @EJB
    UsuarioServico usuarioDao;
    
    private Usuario usuario;
    private Averbacao averbacao;
    private Imovel imovel;
    private Transferencia transferencia;
    
    public ManagerAverbacao() {

    }
    
    @PostConstruct
    public void init(){
        this.averbacao = new Averbacao();
        this.transferencia = new Transferencia();
        this.usuario = new Usuario();
    }

    public Usuario getUsuario() {
        return usuario;
    }

    public void setUsuario(Usuario usuario) {
        this.usuario = usuario;
    }
    
    

    public Transferencia getTransferencia() {
        return transferencia;
    }

    public void setTransferencia(Transferencia transferencia) {
        this.transferencia = transferencia;
    }

    
    public Averbacao getAverbacao() {
        return averbacao;
    }

    public void setAverbacao(Averbacao averbacao) {
        this.averbacao = averbacao;
    }

    public Imovel getImovel() {
        return imovel;
    }

    public void setImovel(Imovel imovel) {
        this.imovel = imovel;
    }
    

    
    public void salvar(){
        try {
            usuario = usuarioDao.verificarUsuarioNome(FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal().getName());
            
            List<Averbacao> lstAverbacao = new ArrayList<Averbacao>();
            int i = 0;
            i = averbacaoServico.findUltima(imovel) == null ? 0 : averbacaoServico.findUltima(imovel).getNumero();
            
            averbacao.setNumero(i + 1);
            averbacao.setDataAverbacao(new Date());
            averbacao.setUsuario(usuario);
            
            Msg.messagemInfo("Salvo com Sucesso !");
            
            lstAverbacao = averbacaoServico.findAverImovel(imovel, "ASC");
            lstAverbacao.add(averbacao);
            imovel.setAverbacao(lstAverbacao);            
            imovelServico.edit(imovel);
            reinit();
            
            
        } catch (Exception ex) {
            Logger.getLogger(ManagerAverbacao.class.getName()).log(Level.SEVERE, null, ex);
            Msg.messagemError("Erro ao Salvar!");
        }
    }
    
    public void reinit(){
        averbacao = new Averbacao();
    }
     public void averbarImovel(Imovel imovelParam){
          imovel = imovelServico.find(imovelParam.getId());;
          
          transferencia = transferenciaServico.verificaPrimeiraTrans(imovelParam.getId());
         
          redirect("criarAverbacao.xhtml");         
     }
     
       
     public void redirect(String string) {
        try {
            FacesContext context = FacesContext.getCurrentInstance();
            context.getExternalContext().redirect(string);
        } catch (IOException ex) {
            Logger.getLogger(ManagerImovel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    
}
